package com.cjy.common.filter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.base.common.utils.StringUtils;

/**
 * User jay.zhoujingjie Time: 上午8:56
 */
public class OmRequestWrap extends HttpServletRequestWrapper {

	public OmRequestWrap(HttpServletRequest request) {
		super(request);
	}

	/**
	 * 过滤字符串
	 * 
	 * @param message
	 * @return
	 * @author jay.zhoujingjie
	 */
	private String filter(String message) {
		/*
		 * return StringUtils.replaceEach(name,// new String[]{"\"", "'", "<",
		 * ">"}, // new String[]{"&quot;", "&acute;", "&lt;", "&gt;"});
		 */
		// return StringEscapeUtils.escapeHtml4(name);
		return StringUtils.filter(message);
	}

	/**
	 * @param name
	 * @return
	 */
	@Override
	public Object getAttribute(String name) {
		Object value = super.getAttribute(name);
		if (value instanceof String) {
			value = filter(String.valueOf(value));
		}
		return value;
	}

	/**
	 * 重写getParameter方法
	 * 
	 * @param name
	 * @return
	 */
	@Override
	public String getParameter(String name) {
		String value = super.getParameter(name);
		if (value == null)
			return null;
		return filter(value);
	}

	/**
	 * @param name
	 * @return
	 */
	@Override
	public String[] getParameterValues(String name) {
		String[] values = super.getParameterValues(name);
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				values[i] = filter(values[i]);
			}
		}
		return values;
	}

	/**
	 * @return
	 */
	@Override
	public Map<String, String[]> getParameterMap() {

		HashMap<String, String[]> paramMap = (HashMap<String, String[]>) super
				.getParameterMap();
		paramMap = (HashMap<String, String[]>) paramMap.clone();

		for (Iterator iterator = paramMap.entrySet().iterator(); iterator
				.hasNext();) {
			Map.Entry<String, String[]> entry = (Map.Entry<String, String[]>) iterator
					.next();
			String[] values = entry.getValue();
			if (values != null) {
				for (int i = 0; i < values.length; i++) {
					values[i] = filter(values[i]);
				}
			}
			entry.setValue(values);
		}
		return paramMap;
	}
}
